#include "BlockQueue.hpp"
#include <pthread.h>
#include <unistd.h>
#include "Thread.hpp"
#include <functional>

using namespace BlockQueueModule;
using namespace ThreadModule;

const int ppnum = 10;
const int pcnum = 5;

void *Consumer(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);
    while (true)
    {
        int data;

        bq->Pop(&data);

        printf("Consumer, 消费了一个数据: %d\n", data);
    }
}
int pdata = 10;
void *Productor(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);

    while (true)
    {
        sleep(2);

        // 2. 生产到bq中
        bq->Equeue(pdata);

        pdata++;
        // printf("producter 生产了一个数据: %d\n", pdata);
    }
}

int main()
{

    BlockQueue<int> *bq = new BlockQueue<int>(5); // 共享资源 -> 临界资源

    std::vector<Thread> cthread;
    std::vector<Thread> pthread;

    for (int i = 0; i < ppnum; i++)
        pthread.emplace_back(std::bind(Productor, bq));
    for (int i = 0; i < pcnum; i++)
        cthread.emplace_back(std::bind(Consumer, bq));

    for (auto &thread : pthread)
    {
        thread.Start();
    }
    for (auto &thread : cthread)
    {
        thread.Start();
    }

    for (auto &thread : pthread)
    {
        thread.Join();
    }
    for (auto &thread : cthread)
    {
        thread.Join();
    }

    delete bq;

    return 0;
}